<?php

use Illuminate\Database\Seeder;
use App\Models\User;
use Illuminate\Support\Facades\Hash;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 获取 Faker 实例
        $faker = app(Faker\Generator::class);

        // 头像假数据
        $avatars = [
            'http://pic13.nipic.com/20110409/7119492_114440620000_2.jpg',
            'http://pic13.nipic.com/20110409/7119492_114440620000_2.jpg',
            'http://pic13.nipic.com/20110409/7119492_114440620000_2.jpg',
            'http://pic13.nipic.com/20110409/7119492_114440620000_2.jpg',
            'http://photocdn.sohu.com/20120128/Img333056814.jpg',
            'http://photocdn.sohu.com/20120128/Img333056814.jpg',
        ];

        // 生成数据集合
        $users = factory(User::class)
            ->times(10)
            ->make()
            ->each(function ($user, $index)
            use ($faker, $avatars)
            {
                // 从头像数组中随机取出一个并赋值
                $user->avatar = $faker->randomElement($avatars);
            });

        // 让隐藏字段可见，并将数据集合转换为数组
        $user_array = $users->makeVisible(['password', 'remember_token'])->toArray();

        // 插入到数据库中
        User::insert($user_array);

        // 单独处理第一个用户的数据
        $user = User::find(1);
        $user->name = 'Max-otm';
        $user->email = '15979076441@163.com';
        $user->avatar = 'avatar/1_1558144900_9p30BYREnq.png';
        $user->password = Hash::make('123456');
        // 初始化用户角色，将 1 号用户指派为『站长』
        $user->assignRole('Founder');
        $user->save();
        // 将 2 号用户指派为『管理员』
        $user = User::find(2);
        $user->assignRole('Maintainer');
        $user->name = 'Maxotm';
        $user->email = '1635358408@qq.com';
        $user->avatar = 'avatar/4ae1b731cd2b91f29daf45b2ac6d46b4.png';
        $user->password = Hash::make('123456');
        $user->save();

    }
}
